package com.young.mhl.view;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.young.mhl.domain.*;
import com.young.mhl.service.BillService;
import com.young.mhl.service.DiningTableService;
import com.young.mhl.service.EmployeeService;
import com.young.mhl.service.MenuService;
import com.young.mhl.utils.Utility;

import java.util.List;

/**
 * @Author: young
 * @Date: 2021-12-04 14:28
 * @Description: 主界面
 */
public class mhlView {
	// 控制是否退出菜单
	private boolean loop = true;
	// 接收用户的输入
	private String key = "";
	// 定义EmployeeService属性
	private EmployeeService employeeService = new EmployeeService();
	// 定义DiningTableService属性
	private DiningTableService diningTableService = new DiningTableService();
	// 定义MenuService属性
	private MenuService menuService = new MenuService();
	// 定义BillService属性
	private BillService billService = new BillService();
	
	public static void main(String[] args) {
		mhlView mhlView = new mhlView();
		mhlView.mainMenu();
	}
	
	// 显示餐桌状态
	public void listDiningTable(){
		List<DiningTable> list = diningTableService.list();
		System.out.println("餐桌编号\t\t餐桌状态");
		for (DiningTable diningTable : list) {
			System.out.println(diningTable);
		}
		System.out.println("======显示完毕======");
	}
	
	// 完成订座
	public void orderDiningTable(){
		System.out.println("======预定餐桌======");
		System.out.println("请选择要预定的餐桌的编号(-1退出)：");
		int orderId = Utility.readInt();
		if (orderId == -1){
			System.out.println("======取消预定餐桌======");
			return;
		}
		// 该方法得到的是Y或N
		char key = Utility.readConfirmSelection();
		if (key == 'Y') {
			DiningTable diningTableById = diningTableService.getDiningTableById(orderId);
			if (diningTableById == null){
				System.out.println("======预定的餐桌不存在======");
				return;
			}
			// 判断该餐桌的状态是否为"空"
			if (!("空".equals(diningTableById.getState()))){
				System.out.println("======餐桌已预定或者就餐======");
				return;
			}
			// 接收预定信息
			System.out.print("预定人的名字：");
			String orderName = Utility.readString(50);
			System.out.print("预定人的电话：");
			String orderTel = Utility.readString(50);
			// 更新餐桌的状态
			if (diningTableService.orderDiningTable(orderId,orderName,orderTel)){
				System.out.println("======预定成功======");
			} else {
				System.out.println("======预定失败======");
			}
		} else {
			System.out.println("======取消预定餐桌======");
			return;
		}
	}
	
	// 显示所有菜品
	public void menuList(){
		List<Menu> list = menuService.list();
		System.out.println("菜品编号\t\t菜品名称\t\t类别\t\t价格");
		for (Menu menu : list) {
			System.out.println(menu);
		}
		System.out.println("======显示完毕======");
	}
	
	// 完成点餐
	public void orderMenu(){
		System.out.println("======点餐服务======");
		System.out.print("请输入点餐的桌号(-1退出)：");
		int orderDiningTableId = Utility.readInt();
		if (orderDiningTableId == -1){
			System.out.println("======取消点餐======");
			return;
		}
		System.out.print("请输入点餐的菜品编号(-1退出)：");
		int orderMenuId = Utility.readInt();
		if (orderMenuId == -1){
			System.out.println("======取消点餐======");
			return;
		}
		System.out.print("请输入点餐的菜品数量(-1退出)：");
		int orderNums = Utility.readInt();
		if (orderMenuId == -1){
			System.out.println("======取消点餐======");
			return;
		}
		
		//验证餐桌号是否存在
		DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
		if (diningTable == null){
			System.out.println("======餐桌号不存在======");
			return;
		}
		//验证菜品编号
		Menu menu = menuService.getMenuById(orderMenuId);
		if (menu == null){
			System.out.println("======菜品号不存在======");
			return;
		}
		//点餐
		if (billService.orderMenu(orderMenuId,orderNums,orderDiningTableId)){
			System.out.println("======点餐成功======");
		} else {
			System.out.println("======点餐失败======");
		}
	}
	
	// 查看账单
	//	public void billList(){
	//		List<Bill> list = billService.list();
	//		System.out.println("编号\t\t菜品号\t\t菜品数量\t\t金额\t\t桌号\t\t日期\t\t\t\t状态");
	//		for (Bill bill : list) {
	//			System.out.println(bill);
	//		}
	//		System.out.println("======显示完毕======");
	//	}
	
	//查看账单信息和菜品名
	public void billList(){
		List<MultiTableBean> multiTableBeans = billService.list2();
		System.out.println("编号\t\t菜品号\t\t菜品数量\t\t金额\t\t桌号\t\t日期\t\t\t\t状态\t\t菜品名\t\t价格");
		for (MultiTableBean multiTableBean : multiTableBeans) {
			System.out.println(multiTableBean);
		}
		System.out.println("======显示完毕======");
	}
	
	// 结账
	public void payBill(){
		System.out.println("======结账服务======");
		System.out.println("请选择要结账的餐桌编号(-1退出)：");
		int diningTableId = Utility.readInt();
		if (diningTableId == -1){
			System.out.println("======取消结账======");
		}
		//验证餐桌是否存在
		DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
		if (diningTable == null){
			System.out.println("======结账的餐桌号不存在======");
			return;
		}
		//是否有需要结账的账单
		boolean bill = billService.hasPayBillByDiningTableId(diningTableId);
		if (!bill){
			System.out.println("======没有要结账的账单======");
			return;
		}
		System.out.println("结账的方式(现金/支付宝/微信)回车表示退出：");
		String payMode = Utility.readString(20, "");
		if (payMode==""){
			System.out.println("======取消结账======");
			return;
		}
		char c = Utility.readConfirmSelection();
		if (c=='Y'){
			boolean payBill = billService.payBill(diningTableId, payMode);
			if (payBill){
				System.out.println("======结账成功======");
			} else {
				System.out.println("======结账失败======");
			}
		} else {
			System.out.println("======取消结账======");
		}
		
	}
	
	// 显示主菜单
	public void mainMenu(){
		while (loop){
			System.out.println("=======满汉楼======");
			System.out.println("\t\t 1 登陆满汉楼");
			System.out.println("\t\t 2 退出满汉楼");
			System.out.print("请输入你的选择：");
			key = Utility.readString(1);
			switch (key){
				case "1":
					System.out.println("请输入员工号：");
					String empId = Utility.readString(50);
					System.out.println("请输入密码：");
					String pwd = Utility.readString(50);
					// 到数据库去判断
					Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
					if (employee != null){
						System.out.println("登陆成功---"+employee.getName());
						//显示二级菜单
						while (loop){
							System.out.println("=======满汉楼(二级菜单)======");
							System.out.println("\t\t 1 显示餐桌状态");
							System.out.println("\t\t 2 预定餐桌");
							System.out.println("\t\t 3 显示所有菜品");
							System.out.println("\t\t 4 点餐");
							System.out.println("\t\t 5 查看账单");
							System.out.println("\t\t 6 结账");
							System.out.println("\t\t 9 退出满汉楼");
							System.out.print("请输入你的选择：");
							key = Utility.readString(1);
							switch (key){
								case "1":
									listDiningTable();
									break;
								case "2":
									orderDiningTable();
									break;
								case "3":
									menuList();
									break;
								case "4":
									orderMenu();
									break;
								case "5":
									billList();
									break;
								case "6":
									payBill();
									break;
								case "9":
									loop = false;
									break;
								default:
									System.out.println("输入有误，请重新输入");
									break;
							}
						}
					} else {
						System.out.println("登陆失败");
					}
					break;
				case "2":
					loop = false;
					break;
				default:
					System.out.println("输入有误，请重新输入");
					break;
			}
		}
		System.out.println("你退出了满汉楼系统");
	}
	
	
}
